package com.ruyuan.eshop.construction.service.impl;

import com.ruyuan.eshop.construction.converter.CategoryInfoConverter;
import com.ruyuan.eshop.construction.domain.dto.CategoryInfoDTO;
import com.ruyuan.eshop.construction.domain.dto.CategoryInfoTreeDTO;
import com.ruyuan.eshop.construction.domain.dto.CategoryResultDTO;
import com.ruyuan.eshop.construction.domain.entity.CategoryInfoDO;
import com.ruyuan.eshop.construction.domain.request.CategoryRequest;
import com.ruyuan.eshop.construction.domain.request.QueryCategoryListRequest;
import com.ruyuan.eshop.construction.domain.request.QueryCategoryRequest;
import com.ruyuan.eshop.construction.repository.CategoryRepository;
import com.ruyuan.eshop.construction.service.CategoryInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商品品类信息
 *
 * @author zhonghuashishan
 */
@Service
public class CategoryInfoServiceImpl implements CategoryInfoService {

    @Resource
    private CategoryRepository categoryRepository;

    @Resource
    private CategoryInfoConverter categoryInfoConverter;

    /**
     *  查询品类树
     * @param categoryQueryRequest
     * @return
     */
    @Override
    public List<CategoryInfoTreeDTO> selectTree(QueryCategoryRequest categoryQueryRequest) {
        return categoryInfoConverter.converterTreeList(categoryRepository.selectTree(categoryQueryRequest));
    }

    /**
     * 查询某个层级下的品类树(默认不带条件查询父类)
     * @param categoryQueryRequest
     * @return
     */
    @Override
    public List<CategoryInfoDTO> selectChild(QueryCategoryRequest categoryQueryRequest) {
        // 查询某个层级的品类树
        List<CategoryInfoDO> categoryInfoList = categoryRepository.listBy(categoryQueryRequest);
        // 返回查询结果
        return categoryInfoConverter.converterList(categoryInfoList);
    }

    /**
     *   保存/修改 品类信息
     * @param categoryRequest
     * @return
     */
    @Override
    public CategoryResultDTO saveCategory(CategoryRequest categoryRequest) {
        // 保存 品类树
        categoryRepository.saveOrUpdate(categoryRequest);
        // 返回结果信息
        return new CategoryResultDTO(Boolean.TRUE);
    }

    /**
     *  查询品类信息列表
     * @param categoryListRequest
     * @return
     */
    @Override
    public List<CategoryInfoDTO> selectListByLike(QueryCategoryListRequest categoryListRequest) {
        return categoryInfoConverter.converterList(categoryRepository.selectListByLike(categoryListRequest));
    }

}
